package main

import (
	"crypto/md5"
	"encoding/json"
	"fmt"
	"github.com/spf13/pflag"
	"net/url"
	"os"
	"sort"
	"time"
)

func GetOpenapiSignFormatString(d map[string]interface{}) (string, error) {
	var clientKey = "80002"
	var appSecret = "eLZHr7C8cY0gBDO2HnSfLboloRkNtl7q"
	d["sign_time"] = fmt.Sprintf("%d", time.Now().Unix())

	// 获取参数Key序列
	kList := []string{}
	for k, _ := range d {
		kList = append(kList, k)
	}
	sort.Strings(kList)

	// 根据参数Key序列拼接内容
	signStr := ""
	for _, k := range kList {
		signStr = fmt.Sprintf("%s%s%s", signStr, k, d[k])
	}
	signStr = fmt.Sprintf("client_key%s%sclient_secret%s", clientKey, signStr, appSecret)

	// 加密拼接内容
	d["sign"] = fmt.Sprintf("%x", md5.Sum([]byte(signStr)))
	d["client_key"] = clientKey

	// 转为字符串
	ds, err := json.Marshal(d)
	if err != nil {
		return "", err
	} else {
		return string(ds), nil
	}
}

func MapToForm(d map[string]interface{}) url.Values {
	data := make(url.Values)
	for k, v := range d {
		switch t := v.(type) {
		case string, int, float32, float64:
			data[k] = []string{fmt.Sprintf("%v", t)}
		case []string:
			data[k] = t
		}
	}
	return data
}

func main() {
	var appid string
	fmt.Println(11111, os.Args)
	fs := pflag.NewFlagSet("daprd", pflag.ExitOnError)
	fs.StringVar(&appid, "appid", "", "A unique ID for Dapr. Used for Service Discovery and state")
	_ = fs.Parse(os.Args[1:])
	fmt.Println(os.LookupEnv("DAPRT_APPID"))
}
